package com.hgz.jdbcDemo.dao.impl;

import com.hgz.jdbcDemo.dao.StaffsDAO;
import com.hgz.jdbcDemo.pojo.Staffs;
import com.hgz.jdbcDemo.utils.DBUtil;

import java.util.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 员工记录表数据访类
 * @author 小志
 * @date 2025/2/24 - 15:35
 */
public class StaffsDAOImpl implements StaffsDAO {

    /**
     * 获取所有的员工记录集合
     * @return 所有的员工记录集合
     */
    public List<Staffs> getStaffsList(){
        String sql = "select id,name,age,phone,sta_pos,add_time,update_time from staffs";
        HashMap<String, String> map = new HashMap<>();
        map.put("id","id");
        map.put("name","name");
        map.put("age","age");
        map.put("phone","phone");
        map.put("staPos","sta_pos");
        map.put("addTime","add_time");
        map.put("updateTime","update_time");
        List<Object> objects = new ArrayList<>();
        ArrayList<Staffs> staffsList = null;
        try {
            staffsList = DBUtil.executeQuery(sql, objects, map, Staffs.class);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  staffsList;
    }

    /**
     * 修改员工信息
     * @param staffs 需要修改的员工信息
     * @return 返回是否成功
     */
    public boolean updateStaffs(Staffs staffs){
        boolean res = false;
        if(staffs == null || staffs.getId() == null){
            return res;
        }
        StringBuilder sql = new StringBuilder("update staffs set ");
        List<Object> objects = new ArrayList<>();

        //判断当前有没有参数需要进行更改
        boolean flag = false;
        if(staffs.getName() != null){
            sql.append("name=?,");
            objects.add(staffs.getName());
            flag = true;
        }

        if(staffs.getAge() != null){
            sql.append("age=?,");
            objects.add(staffs.getAge());
            flag = true;
        }

        if(staffs.getPhone() != null){
            sql.append("phone=?,");
            objects.add(staffs.getPhone());
            flag = true;
        }

        if(staffs.getStaPos() != null){
            sql.append("sta_pos=?,");
            objects.add(staffs.getStaPos());
            flag = true;
        }

        sql.append(" update_time=? where id = ?");
        objects.add(new Date());
        objects.add(staffs.getId());

        try {
            if( DBUtil.executeUpdate(sql.toString(), objects) > 0){
               res = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return res;
    }

    /**
     * 添加用户并获取自增id
     * @param staffs 添加的对象
     * @return 返回自增的id
     */
    public Integer addStaffs(Staffs staffs){
        Integer res = 0;
        if(staffs == null){
            return res;
        }

        StringBuffer sql = new StringBuffer("insert into staffs(name,age,phone,sta_pos) values(?,?,?,?)");
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(staffs.getName());
        objects.add(staffs.getAge());
        objects.add(staffs.getPhone());
        objects.add(staffs.getStaPos());

        try {
            res = DBUtil.executeUpdateAddGetID(sql.toString(),objects);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return res;
    }

    /**
     * 删除当前员工信息
     * @param id 用户id
     * @return 是否删除成功
     */
    public boolean deleteStaffs(Integer id){
        boolean res = false;
        String sql = "delete from staffs where id = ?";

        ArrayList<Object> objects = new ArrayList<>();
        objects.add(id);

        try {
            if(DBUtil.executeUpdate(sql,objects)>0){
                res = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return res;
    }
}
